﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Core.CoreImpl.Repositories.Interface;

namespace EzDesk.Core.CoreImpl.Repositories
{
    public class UserRepository : IUserRepository, IRepository<UserProfile>
    {
        private EzDeskDataContext _db;

        public UserRepository(EzDeskDataContext db)
        {
            this._db = db;
        }

        // todo: 使用DI重构
        public UserRepository()
            : this(new EzDeskDataContext())
        { }


        #region IRepository<UserProfile> Members

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }

        public Guid Add(UserProfile model, bool saveChangs = false)
        {
                var entity = model.ToEntity();
                _db.ez_UserProfiles.InsertOnSubmit(entity);

                if (saveChangs)
                {
                    SaveChanges();
                }

                return entity.UserId;
        }

        public UserProfile GetById(Guid id)
        {
            UserProfile user = null;
            if (id != null)
            {
                user = _db.ez_UserProfiles.Where(p => p.UserId == id)
                    .SingleOrDefault()
                    .ToModel();
            }
            return user;
        }

        public IQueryable<UserProfile> GetAll()
        {
            return from user in _db.ez_UserProfiles
                   select new UserProfile()
                   {
                       Id = user.UserId,
                       Name = user.Name,
                       UserName = user.UserName,
                       DeptId = user.DeptId,
                       PositionId = user.PosId,
                       ReportId = user.ReportId,
                       ReportPath = user.ReportPath
                   };
        }

        public void Delete(Guid id, bool saveChangs = false)
        {
                var entity = _db.ez_UserProfiles.Where(p => p.UserId == id)
                    .SingleOrDefault();
                _db.ez_UserProfiles.DeleteOnSubmit(entity);
                if (saveChangs)
                {
                    SaveChanges();
                }

        }

        public void Update(UserProfile model, bool saveChangs = false)
        {
            var entity = _db.ez_UserProfiles.FirstOrDefault(p => p.UserId == model.Id);
            if (entity != null)
            {
                ModelConvert.CopyValueUserProfile(entity, model);
            }

            if (saveChangs)
            {
                SaveChanges();
            }
        }

        #endregion

        #region IUserRepository Members

        public UserProfile CreateEmptyUser(string userName)
        {
            ez_UserProfile entity =
                new ez_UserProfile()
                {
                    UserId = Guid.NewGuid(),
                    UserName = userName,
                };
            _db.ez_UserProfiles.InsertOnSubmit(entity);
            _db.SubmitChanges();

            return entity.ToModel();
        }

        public IQueryable<SimpleUser> GetAllSimpleUserInfo()
        {
            return from user in _db.ez_UserProfiles
                   let dept = new Dept
                   {
                       Id = user.ez_Dept.DeptId,
                       DeptName = user.ez_Dept.DeptName,
                       GroupId = user.ez_Dept.GroupId,
                   }
                   select new SimpleUser
                   {
                       Id = user.UserId,
                       Name = user.Name,
                       UserName = user.UserName,
                       Dept = dept,
                   };
        }

        public SimpleUser GetSimpleUserById(Guid userId)
        {
            SimpleUser user = null;
            if (userId != null)
            {
                user = _db.ez_UserProfiles.Where(p => p.UserId == userId)
                       .SingleOrDefault()
                       .ToSimpleUser();                
            }
            return user;
        }

        #endregion
    }
}
